package com.acme.bankapp.service.bank;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import com.acme.bankapp.Exception.ClientExistsException;
import com.acme.bankapp.domain.bank.*;


public class BankService{
	public void addClient(Bank bank, Client client) throws ClientExistsException{
		ArrayList<Client> cl=bank.getClients();
		for(Client c: cl){
			if(c.getName().equals(client.getName())){
				throw new ClientExistsException(c.getName());
			}				
		}
		bank.addClienT(client);
	}
	 public void saveBank(Bank bank) throws IOException,FileNotFoundException {
         FileOutputStream fos= new FileOutputStream("C:\\eclipse\\workspace\\lab13\\src\\Bank.ser");
         ObjectOutputStream oos= new ObjectOutputStream(fos);
         
         oos.writeObject(bank);
         oos.close();
         fos.close();
      }
	
	public Bank readBank() throws IOException,FileNotFoundException, ClassNotFoundException{
		FileInputStream fis = new FileInputStream("C:\\eclipse\\workspace\\lab13\\src\\Bank.ser");
		ObjectInputStream ois = new ObjectInputStream(fis);	
		
		Bank bank = (Bank)ois.readObject();
		ois.close();
		fis.close();
		return bank;
	}
	
	public void printMaximumAmountToWithdraw(Bank bank, Client cl){
		System.out.println("Maximum posible means = " + cl.getAccount().maximumAmountToWithdraw());
	}
}
